package edu.cdnu.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import edu.cdnu.dao.CourseDao;
import edu.cdnu.pojo.CourseClassPojo;
import edu.cdnu.pojo.CoursePojo;
import edu.cdnu.pojo.CourseStudentPojo;
import edu.cdnu.pojo.CourseTeacherPojo;
import edu.cdnu.service.CourseService;


@Service
public class CourseServiceImpl implements CourseService {

	@Autowired
	private CourseDao courseDao;
	
	@Override
	public List<CoursePojo> selectCourseListByCourseId(Integer courseId) {
		return courseDao.selectCourseListByCourseId(courseId);
	}

	@Override
	public List<CourseTeacherPojo> selectCourseTeacherRelationshipListByCourseId(int courseId) {
		return courseDao.selectCourseTeacherRelationshipListByCourseId(courseId);
	}

	@Override
	public List<CourseClassPojo> selectCourseClassRelationshipListByCourseId(int courseId) {
		return courseDao.selectCourseClassRelationshipListByCourseId(courseId);
	}

	@Override
	public List<CourseStudentPojo> selectCourseStudentRelationshipListByCourseId(int courseId) {
		return courseDao.selectCourseStudentRelationshipListByCourseId(courseId);
	}

	@Override
	public int insertCourseReturnId(CoursePojo coursePojo) {
		// 课程名称不能为空
		if(coursePojo!=null&&coursePojo.getName()!=null&&!"".equals(coursePojo.getName())) {
			// mybatis insert不会返回id
			courseDao.insertCourse(coursePojo);
			// 但是在这能获取到id
			return coursePojo.getId();
		}
		return -1;
	}

	@Override
	public void updateCourseByCourseId(CoursePojo coursePojo) {
		// id不能为空
		if(coursePojo != null && coursePojo.getId() != null) {
			courseDao.updateCourseByCourseId(coursePojo);
		}
	}

	@Override
	public List<CourseTeacherPojo> selectCourseTeacherListByCourseTeacherPojo(CourseTeacherPojo courseTeacherPojo) {
		if(courseTeacherPojo != null 
				&& (courseTeacherPojo.getId()!=null 
				|| courseTeacherPojo.getCourseId() != null 
				|| courseTeacherPojo.getTeacherId() != null)) {
			return courseDao.selectCourseTeacherListByCourseTeacherPojo(courseTeacherPojo);
		}
		return null;
	}

	@Override
	public void insertCourseTeacher(CourseTeacherPojo courseTeacherPojo) {
		if(courseTeacherPojo != null && courseTeacherPojo.getCourseId() != null && courseTeacherPojo.getTeacherId() != null) {
			courseDao.insertCourseTeacher(courseTeacherPojo);
		}
	}

	@Override
	public void insertCourseTeacherByList(List<CourseTeacherPojo> courseTeacherPojoList) {
		if(courseTeacherPojoList.size()>0) {
			courseDao.insertCourseTeacherByList(courseTeacherPojoList);
		}
	}

	@Override
	public void deleteCourseTeacherById(int id) {
		courseDao.deleteCourseTeacherById(id);
		
	}

	@Override
	public void deleteCourseTeacherByIdList(List<Integer> idList) {
		if(idList.size()>0) {
			courseDao.deleteCourseTeacherByIdList(idList);
		}
	}

	@Override
	public List<Integer> selectTeacherIdListByCourseId(int courseId) {
		return courseDao.selectTeacherIdListByCourseId(courseId);
	}

	@Override
	public void deleteCourseTeacherByCourseTeacherPojo(CourseTeacherPojo courseTeacherPojo) {
		if(courseTeacherPojo != null 
//				课程id和教师id都不能为空 不然删多了
				&& courseTeacherPojo.getTeacherId() != null
				&& courseTeacherPojo.getCourseId() != null) {
			courseDao.deleteCourseTeacherByCourseTeacherPojo(courseTeacherPojo);
		}
	}

	@Override
	public void insertCourseClassByList(List<CourseClassPojo> courseClassList) {
		if(courseClassList.size()>0) {
			courseDao.insertCourseClassByList(courseClassList);
		}
	}

	@Override
	public List<Integer> selectCourseIdListByTeacherId(int teacherId) {
		return courseDao.selectCourseIdListByTeacherId(teacherId);
	}

	@Override
	public List<CoursePojo> selectCourseListByCourseIdList(List<Integer> idList) {
		if(idList.size()>0) {
			return courseDao.selectCourseListByCourseIdList(idList);
		}
		return null;
	}

	@Override
	public CoursePojo selectCourseItemByCourseId(int courseId) {
		// 有id肯定只有一个，直接返回第一个就行了
		return courseDao.selectCourseListByCourseId(courseId).get(0);
	}

	@Override
	public List<Integer> selectClassIdListByCourseIdList(List<Integer> idList) {
		if(idList != null && idList.size()>0) {
			return courseDao.selectClassIdListByCourseIdList(idList);
		}
		return null;
	}

	@Override
	public List<Integer> selectCourseIdListByClassIdList(int classId) {
		return courseDao.selectCourseIdListByClassIdList(classId);
	}

	@Override
	public CourseStudentPojo selectCourseStudentPojoByCourseStudentPojo(CourseStudentPojo courseStudentPojo) {
		if(courseStudentPojo == null) {
			return null;
		}
		return courseDao.selectCourseStudentPojoByCourseStudentPojo(courseStudentPojo);
	}

	@Override
	public CourseStudentPojo selectCourseStudentPojoByCourseIdAndStudentId(int courseId, int studentId) {
		CourseStudentPojo courseStudentPojo=new CourseStudentPojo();
		courseStudentPojo.setCourseId(courseId);
		courseStudentPojo.setStudentId(studentId);
		return courseDao.selectCourseStudentPojoByCourseStudentPojo(courseStudentPojo);
	}

	@Override
	public void addOrUpdateCourseStudent(CourseStudentPojo courseStudentPojo) {
		if(courseStudentPojo==null) {
			return ;
		}else if(courseStudentPojo.getId()==null) {
			// 没得id新增
			courseDao.insertCourseStudent(courseStudentPojo);
		}else {
			// 有id修改
			courseDao.updateCourseStudent(courseStudentPojo);
		}
		
	}

	@Override
	public void addCourseStudentVisitTime(int courseStudentId) {
		courseDao.addCourseStudentVisitTime(courseStudentId);
	}

	@Override
	public List<CourseStudentPojo> selectCourseStudentListByCourseId(int courseId) {
		return courseDao.selectCourseStudentListByCourseId(courseId);
	}

}
